/***************************************** 
 * Desc: UGUI的根节点，项目唯一一个根UGUI节点
 * @Author: GSan
 * @Email: 2087653648@qq.com
 * CreateDate: 2020-07-10 11:34:11
*****************************************/

using Common.UGUI.Base;
using UnityEngine;

namespace Common.UGUI.View {

	public class UGUIRoot : UGUIView {

        #region 域

        private bool _ifHasMarkedDontDestroy = false;

        #endregion

        #region 覆盖Unity生命周期

        protected override void OnAwake() {

            if (!_ifHasMarkedDontDestroy) {

                if (Application.isPlaying) {

                    DontDestroyOnLoad(GameObject);
                }

                _ifHasMarkedDontDestroy = true;
            }
        }

        #endregion

        /// <summary>
        /// UGUIRoot是无法隐藏的，因为只是一个概念节点
        /// </summary>
        public override bool IsActive {
            get {
                return base.IsActive;
            }

            protected set { base.SetActive(true); }
        }

        public override void SetParent(UGUIView parent, bool ifRemoveFromParent = true) {

            base.SetParent(null);
        }

        /// <summary>
        /// 只能加载Canvas
        /// </summary>
        protected override bool AddChild(UGUIView child) {

            UGUICanvas canvas = child as UGUICanvas;

            if (canvas != null) {

                base.AddChild(canvas);

                return true;
            } else {

                Debug.LogError("UGUIRoot add children error: child must be UGUICanvas.");

                return false;
            }
        }

        /// <summary>
        /// Root级，不存在父节点，直接为null，其它所有节点都在Root节点下
        /// </summary>
        public override void AutoFindParent(bool ifForceFind = false) {

            _parent = null;
        }

    }
}
